package com.ruoyi.ucenter.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    private Pattern pattern;
    private Matcher matcher;


    public EmailValidator() {
        pattern = Pattern.compile(EMAIL_PATTERN);
    }

    /**
     * 验证给定的字符串是否是有效的电子邮件地址。
     *
     * @param email 要验证的电子邮件地址字符串。
     * @return 如果是有效的电子邮件地址则返回true，否则返回false。
     */
    public boolean validate(final String email) {
        matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static void main(String[] args) {
        EmailValidator validator = new EmailValidator();

        // 测试邮箱地址
        String[] testEmails = {
                "example@example.com",
                "invalid-email@com",
                "user.name+tag+sorting@example.co.uk",
                "not_an_email",
                "2305982152@qq.com"
        };

        for (String email : testEmails) {
            System.out.println(email + ": " + validator.validate(email));
        }
    }
}